/*
----------------------------------------------------------------------------
Copyright (c) 2005 - 2009, Wangxing SHI	<swxlion@gmail.com>

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above
copyright notice, this list of conditions and the
following disclaimer.

* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials
provided with the distribution.

* Neither the copyright holder's name nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior written
permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
----------------------------------------------------------------------------
*/

/*-------------------------- TBasalPoolMap.hpp ----------------------------*/
/*===========================================================================
FILE: TBasalPoolMap.hpp
Author: Wangxing SHI (swxlion)
Version: 0.0.1 
Date: 2009-07-04

File Description:
    C++ template for basal pool map.

Dependencies:
    Platform: Platform Independent.

NOTE:
	The implement can't be used with Memory Pool.
History:
version        author        date       description
----------------------------------------------------
0.0.1          swxlion       09-07-04   Created the first version.
===========================================================================*/
/*===============================================================================
SECTIONS INCLUDED
=============================================================================== */
#ifdef __LUNA_LOG__TBASAL_POOL_MAP__POOL_TREE__FIND__
				if( !pRoot )
					return NULL;

				RBNode*	pFind = pRoot;
				while( pFind )
				{
					if( pFind->Data == str )
						return pFind;
					else if( pFind->Data > str )
						pFind = pFind->Left;
					else
						pFind = pFind->Right;
				}
				return NULL;
#endif

/*===============================================================================
FORMAL CONTENT
=============================================================================== */
#ifndef _LUNA_LOG__TBASAL_POOL_MAP_H_
#define _LUNA_LOG__TBASAL_POOL_MAP_H_

/*===============================================================================
INCLUDES AND VARIABLE DEFINITIONS
=============================================================================== */
#include "GDST/TRBTree.h"
#include <new>
#include <string>
/*===============================================================================
CLASS & STRUCTURE DEFINITIONS
=============================================================================== */
namespace LunaLog
{
	template<typename Type>
	class TBasalPoolMap
	{
	public:
		class Pair
		{
		public:
			std::string		name;
			Type			content;

			Pair(): name(), content()
			{
			}
			Pair(const std::string &str, const Type &c): name(str), content(c)
			{
			}

			//-- Overload the operators of "<", ">", "<=", ">=", and "==".
			//-- For class Pair &
			bool operator > ( const Pair &r ) const
			{
				return (name > r.name) ? true : false;
			}
			bool operator < ( const Pair &r ) const
			{
				return (name < r.name) ? true : false;
			}
			bool operator <= ( const Pair &r ) const
			{
				return (name <= r.name) ? true : false;
			}
			bool operator >= ( const Pair &r ) const
			{
				return (name >= r.name) ? true : false;
			}
			bool operator != ( const Pair &r ) const
			{
				return (name != r.name) ? true : false;
			}
			bool operator == ( const Pair &r ) const
			{
				return (name == r.name) ? true : false;
			}

			//-- For std::tring &
			bool operator > ( const std::string &r ) const
			{
				return (name > r) ? true : false;
			}
			bool operator < ( const std::string &r ) const
			{
				return (name < r) ? true : false;
			}
			bool operator <= ( const std::string &r ) const
			{
				return (name <= r) ? true : false;
			}
			bool operator >= ( const std::string &r ) const
			{
				return (name >= r) ? true : false;
			}
			bool operator != ( const std::string &r ) const
			{
				return (name != r) ? true : false;
			}
			bool operator == ( const std::string &r ) const
			{
				return (name == r) ? true : false;
			}

			//-- For std::tring *
			bool operator > ( const std::string *r ) const
			{
				return (name > *r) ? true : false;
			}
			bool operator < ( const std::string *r ) const
			{
				return (name < *r) ? true : false;
			}
			bool operator <= ( const std::string *r ) const
			{
				return (name <= *r) ? true : false;
			}
			bool operator >= ( const std::string *r ) const
			{
				return (name >= *r) ? true : false;
			}
			bool operator != ( const std::string *r ) const
			{
				return (name != *r) ? true : false;
			}
			bool operator == ( const std::string *r ) const
			{
				return (name == *r) ? true : false;
			}

			//-- For char *
			bool operator > ( const char *r ) const
			{
				return (name > r) ? true : false;
			}
			bool operator < ( const char *r ) const
			{
				return (name < r) ? true : false;
			}
			bool operator <= ( const char *r ) const
			{
				return (name <= r) ? true : false;
			}
			bool operator >= ( const char *r ) const
			{
				return (name >= r) ? true : false;
			}
			bool operator != ( const char *r ) const
			{
				return (name != r) ? true : false;
			}
			bool operator == ( const char *r ) const
			{
				return (name == r) ? true : false;
			}
		};

		typedef Pair			*iterator;

	private:
		class PoolTree : public ASDL::TRBTree<Pair>
		{
		public:
			RBNode *	Find(const std::string &str)
			{
#define __LUNA_LOG__TBASAL_POOL_MAP__POOL_TREE__FIND__
#include "TBasalPoolMap.hpp"
#undef __LUNA_LOG__TBASAL_POOL_MAP__POOL_TREE__FIND__
			}

			RBNode *	Find(const std::string *str)
			{
#define __LUNA_LOG__TBASAL_POOL_MAP__POOL_TREE__FIND__
#include "TBasalPoolMap.hpp"
#undef __LUNA_LOG__TBASAL_POOL_MAP__POOL_TREE__FIND__
			}

			RBNode *	Find( const char *str)
			{
#define __LUNA_LOG__TBASAL_POOL_MAP__POOL_TREE__FIND__
#include "TBasalPoolMap.hpp"
#undef __LUNA_LOG__TBASAL_POOL_MAP__POOL_TREE__FIND__
			}
		};

		PoolTree			clsTree;

	public:
		TBasalPoolMap(): clsTree()
		{
		}

		~TBasalPoolMap()
		{
			clear();
		}

		inline bool		insert( std::string &name, Type content );

		inline void		erase( const std::string &name )
		{
			PoolTree::RBNode *		lpDeleteRBNode = clsTree.Find(name);
			clsTree.Delete( lpDeleteRBNode );
		}
		inline void		erase( const std::string *name )
		{
			PoolTree::RBNode *		lpDeleteRBNode = clsTree.Find(name);
			clsTree.Delete( lpDeleteRBNode );
		}
		inline void		erase( const char *name )
		{
			PoolTree::RBNode *		lpDeleteRBNode = clsTree.Find(name);
			clsTree.Delete( lpDeleteRBNode );
		}

		inline void		clear()
		{
			clsTree.Clear();
		}

		inline iterator	begin()
		{
			PoolTree::RBNode *		lpFindNode = clsTree.FindMin();
			if( !lpFindNode )
				return NULL;
			else
				return &(lpFindNode->Data);
		}
		inline iterator	end()
		{
			PoolTree::RBNode *		lpFindNode = clsTree.FindMax();
			if( !lpFindNode )
				return NULL;
			else
				return &(lpFindNode->Data);
		}
		//---------------[ Find ]--------------------------
		inline iterator	find( const std::string &name )
		{
			PoolTree::RBNode *		lpFindNode = clsTree.Find(name);
			if( !lpFindNode )
				return NULL;
			else
				return &(lpFindNode->Data);
		}
		inline iterator	find( const std::string *name )
		{
			PoolTree::RBNode *		lpFindNode = clsTree.Find(name);
			if( !lpFindNode )
				return NULL;
			else
				return &(lpFindNode->Data);
		}
		inline iterator	find( const char *name )
		{
			PoolTree::RBNode *		lpFindNode = clsTree.Find(str);
			if( !lpFindNode )
				return NULL;
			else
				return &(lpFindNode->Data);
		}

		inline iterator FindLarger( iterator iter )
		{
			PoolTree::RBNode *		lpFindNode = clsTree.FindLarger( *iter );
			if( !lpFindNode )
				return NULL;
			else
				return &(lpFindNode->Data);
		}

		inline iterator FindSmaller( iterator iter )
		{
			PoolTree::RBNode *		lpFindNode = clsTree.FindSmaller( *iter );
			if( !lpFindNode )
				return NULL;
			else
				return &(lpFindNode->Data);
		}
	};

	/*===============================================================================
	INLINE FUNCTION DEFINITIONS: Simple Map Functions: Class TSimpleMap
	=============================================================================== */
	template<typename Type>
	bool TBasalPoolMap<Type>::insert(std::string &name, Type content)
	{
		PoolTree::RBNode *		lpInsertNode = new(std::nothrow) RBNODE;
		if( !lpInsertNode )
			return false;

		lpInsertNode->Data.name  = name;
		lpInsertNode->Data.content = content;

		return ((clsTree.Insert( lpInsertNode, false ) == 0) ? true: false);
	}
}

#endif // _LUNA_LOG__TBASAL_POOL_MAP_H_
